﻿
using Storage.Admin.Framework.Controllers.Admin;
using Storage.Admin.Framework.Menu;
using Storage.Core.Encyption;
using Storage.Core.Extensions;
using Storage.Core.MediaItem;
using Storage.Mapping;
using Storage.Mapping.AppStorage;
using Storage.Services.AppStorage;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc; 

namespace Storage.Admin.Areas.Admin.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [RoutePrefix("appstorage")]
    public class AppStorageController : BaseAdminController
    {
        // GET: Admin/AppStorage

        private IAppStorageService _appStorageService;

        public AppStorageController(IAppStorageService appStorageService)
        {
            this._appStorageService = appStorageService;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [Route("", Name = "asppSorageIndex")]
        [Descriper("上传应用列表", true, "menu-icon fa fa-caret-right", FatherResource = "Storage.Admin.Areas.Admin.Controllers.SystemManageController")]
        public ActionResult AppStorageIndex()
        {
            return View(_appStorageService.getAllCache());
        }

        /// <summary>
        /// 编辑应用
        /// </summary>
        /// <returns></returns>
        [Route("edit", Name = "editAppStorage")]
        [Descriper("编辑应用", false, "menu-icon fa fa-caret-right", FatherResource = "Storage.Admin.Areas.Admin.Controllers.AppStorageController.AppStorageIndex")]
        public ActionResult EditAppStorage(Guid? id)
        {
            AppStorageMapping model = new AppStorageMapping();
            if (id != null)
                model = _appStorageService.getById(id.Value);
            return View(model);
        }
        [HttpPost]
        [Route("edit")]
        public JsonResult EditAppStorage(AppStorageMapping model, HttpPostedFileBase file)
        {
            if (!ModelState.IsValid)
            {
                AjaxData.Message = ModelState.getErrMsg();
                return Json(AjaxData);
            }
            if (model.Id == Guid.Empty)
            {
                if (model.WaterType == (int)EnumWaterType.图片水印)
                {
                    if (file == null)
                    {
                        AjaxData.Message = "请选择上传图片文件";
                        return Json(AjaxData);
                    }
                }
            }
            if (file != null)
            {
                if (!file.isImage())
                {
                    AjaxData.Message = "不是图片类型";
                    return Json(AjaxData);
                }
                if (file.isBigSize(102400))
                {
                    AjaxData.Message = "水印图片操过100k";
                    return Json(AjaxData);
                }
                byte[] bt = new byte[file.ContentLength];
                file.InputStream.Read(bt, 0, bt.Length);
                model.WaterImage = bt;
            }
            if (model.Id == Guid.Empty)
            {
                model.AppKey = EncryptorHelper.GetMD5(Guid.NewGuid().ToString());
                model.SecretKey = EncryptorHelper.GetMD5(Guid.NewGuid().ToString());
                var entity = model.toEntity();
                entity.Id = Guid.NewGuid();
                entity.CreationTime = DateTime.Now;
                entity.Creator = WorkContext.CurrentUser.Id;
                var result = _appStorageService.insertStorage(entity);
                AjaxData.Status = result.Item1;
                AjaxData.Message = result.Item2;
            }
            else
            {
                _appStorageService.updateStorage(model);
                AjaxData.Status = true;
            }
            return Json(AjaxData);
        }


        /// <summary>
        /// 查看水印图片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("water-img", Name = "waterImage")]
        [Descriper("查看水印图片", false, "menu-icon fa fa-cogs", FatherResource = "Storage.Admin.Areas.Admin.Controllers.AppStorageController.AppStorageIndex")]
        public FileResult WaterImage(Guid id)
        {
            var app = _appStorageService.getById(id);
            if (app != null && app.WaterImage != null)
            {
                using (MemoryStream ms = new MemoryStream(app.WaterImage))
                {
                    return File(ms.ToArray(), "image/png", app.Id + ".png");
                }
            }
            return File(new byte[0], "image/jpeg", "1.png");
        }


    }
}